$(window).load(function() {
        //页面初始化 
        var OrderId = sessionStorage.getItem("OrderId");
        var sdType = sessionStorage.getItem("OrderType");
        if (sdType === "0") {
            $("#ShouhuoAddress .name").html("收货地址");
            $("#QuHuoAddress .name").html("购买地址");
        } else if (sdType === "1" || sdType === "2") {
            $("#ShouhuoAddress .name").html("收货地址");
            $("#QuHuoAddress .name").html("取货地址");
        } else if (sdType === "5") {
            $("#ShouhuoAddress .name").html("排队信息");
            $("#QuHuoAddress .name").html("排队地址");
            $("#LiCheng .name").html("排队时间");
        }
        //返回上一页
        $("#go_back").click(function() {
                window.location.href = "../index.html";
            })
            //订单时间差
        var timeDiff = 0;
        //调用等待抢单接口
        function getOrderStatus() {
            if (sdType === "0" || sdType === "1" || sdType === "2") {
                $.ajax({
                    type: "get",
                    url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_DetailQiangDan",
                    data: {
                        "OrderId": OrderId
                    },
                    dataType: "json",
                    success: function(data) {
                        //console.log(data)
                        if (data.msgcode == "1") {
                            $("#ShouhuoAddress .content").html(data.data.ShouhuoAddress);
                            $("#QuHuoAddress .content").html(data.data.QuHuoAddress);
                            $("#LiCheng .content").html(data.data.LiCheng + "公里");
                            $("#OrderMonery .content").html("￥" + data.data.OrderMonery + "(服务费￥" + data.data.OrderMonery + ",物品费￥" + data.data.WuPinMoney + ",优惠￥" + data.data.YouMonery + ",小费￥" + data.data.SmallMoney + ")");
                            $("#PeiSongNum").html(data.data.PeiSongNum);
                            $(".time_output").html(data.data.DownTime);
                            timeDiff = data.data.TimeDiff;
                            (timeDiff > 300 ? timeDiff = 300 : timeDiff);
                            peiSongAni();
                        } else {
                            $.toptip(data.msg, 'error');
                            return false
                        };
                    },
                    error: function() {
                        $.alert("获取等待抢单接口错误");
                        return false
                    }
                })
            } else if (sdType === "5") {
                $.ajax({
                    type: "get",
                    url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_DetailBangPai",
                    data: {
                        "OrderId": OrderId
                    },
                    dataType: "json",
                    success: function(data) {

                        if (data.msgcode == "1") {
                            console.log(data)
                            var totalMoney = data.data.Money - data.data.YouHuiMoney;
                            (totalMoney < 0 ? "0" : totalMoney);
                            $("#ShouhuoAddress .content").html(data.data.BangXinxi);
                            $("#QuHuoAddress .content").html(data.data.QIAddress);
                            $("#LiCheng .content").html("排队时长" + data.data.BangShiChang + "分钟");
                            $("#OrderMonery .content").html("￥" + totalMoney + "(服务费￥" + data.data.Money + ",优惠￥" + data.data.YouHuiMoney + ",小费￥" + data.data.AddMoney + ")");
                            $("#PeiSongNum").html(data.data.PeiSongNum);
                            $(".time_output").html(data.data.DownTime);
                            timeDiff = data.data.TimeDiff;
                            (timeDiff > 300 ? timeDiff = 300 : timeDiff);
                            peiSongAni();
                        } else {
                            $.alert(data.msg);
                            return false
                        }
                    },
                    error: function() {
                        $.toptip("获取等待抢单接口(排队)错误", 'error')
                        return false
                    }
                })
            }
        }
        getOrderStatus() //刷新页面获得抢单接口数据
        function peiSongAni() {
            // 开始动画效果
            var leftSeconds = (300 - timeDiff) * 1000;
            (leftSeconds <= 0 ? leftSeconds = 1000 : leftSeconds);
            var progressBarWidth = parseInt($(".progress_bar").css("width"));
            var peiSongleft = (timeDiff / 300) * progressBarWidth * 0.93;
            var leftPercent = (timeDiff / 300) * 100 * 0.93 + "%";
            $(".progress_bar .iconfont").css("left", peiSongleft + "px").animate({
                left: leftPercent
            }, leftSeconds)
        }

        //加载加小费信息； 
        $.ajax({
            type: "get",
            url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_SmaillMoney",
            dataType: "json",
            success: function(data) {
                if (data.msgcode == "1") {
                    //console.log(data);
                    var dataLen = data.data.length;
                    var ulLen = dataLen + 1;
                    var liHeight = parseInt($('#tip_num li').css("height"));
                    $("#tip_num ul").html("");
                    for (var i = 0; i < ulLen; i++) {
                        var liStr = "";
                        if (i == 0) {
                            liStr = "<li class='li_select'>" + data.data[i].Money + "元</li>"
                        } else if (i == dataLen) {
                            liStr = "<li></li>"
                        } else {
                            liStr = "<li>" + data.data[i].Money + "元</li>"
                        }
                        $("#tip_num ul").append(liStr);
                    };
                    for (var k = 0; k < dataLen; k++) {
                        $("#tip_num li").eq(k).data("SmallId", data.data[k].SmallId);
                    }
                } else {
                    $.toptip(data.msg, 'error');
                    return false
                }
            },
            error: function() {
                $.toptip("无法请求到加小费列表数据", 'error');
                return false
            }
        });
        var myScroll;
        var tipPrice = $("#tip_num .li_select").html();
        var goToPrice = 0;
        var SmallId = "";
        var SmallOrderId = "";
        var tipPayBox = 0; //加小费弹出支付层标记 .bg关闭开关
        $("#add_tip").on("click", function() {
                $(".mask").css('display', 'block');
                $('.add_tip_box').css('display', 'block');
                var addPriceScroll = new IScroll('#tip_num', {
                    scrollY: true,
                    listenY: true,
                    tap: true,
                    momentum: false,
                    snap: 'li'
                });
                addPriceScroll.refresh();
                var liHeight = parseInt($('#tip_num li').css("height"));
                addPriceScroll.goToPage(0, goToPrice);
                var maxLen = $("#tip_num li").length - 1;
                addPriceScroll.on('scrollEnd', function() {
                    var addPriceHeight = Math.abs(this.y);
                    addPriceIndex = Math.ceil(addPriceHeight / liHeight);
                    goToPrice = addPriceIndex;
                    if (goToPrice >= maxLen) {
                        goToPrice = maxLen - 1;
                    } else if (goToPrice <= 0) {
                        goToPrice = 0;
                    }
                    $("#tip_num li").removeClass('li_select').eq(goToPrice).addClass('li_select');
                    tipPrice = $("#tip_num .li_select").html();
                    addPriceScroll.goToPage(0, goToPrice)
                    addPriceScroll.refresh();
                })
                $('#tip_num li').off("tap").on("tap", function() {
                    goToPrice = $(this).index();

                    if (goToPrice >= maxLen) {
                        goToPrice = maxLen - 1;
                    }
                    $('#tip_num li').removeClass('li_select').eq(goToPrice).addClass('li_select');
                    addPriceScroll.goToPage(0, goToPrice);
                })
                $('.add_tip_box .confirm').off("click").on("click", function() {
                    $('.mask').css("display", "none");
                    $('.add_tip_box').css('display', 'none');
                    // alert('加小费'+tipPrice);
                    SmallId = $("#tip_num .li_select").data("SmallId");
                    //console.log(SmallId);
                    addPriceScroll.destroy();
                    addPriceScroll = null;
                    //发送小费订单请求;
                    $.ajax({
                        type: "post",
                        url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Add_OrderSmallMoney",
                        data: {
                            "OrderId": OrderId,
                            "SmallId": SmallId
                        },
                        dataType: "json",
                        beforeSend: function() {
                            console.log("正在支付")
                        },
                        success: function(data) {
                            //console.log(data);
                            if (data.msgcode == "1") {
                                //加小费支付弹出层
                                //退出弹出层
                                $('.mask').css("display", "block");
                                $('.tip_pay_box').fadeIn();
                                $(".tip_pay_box .form_submit").html("小费￥" + data.data.SmallMoney);
                                tipPayBox = 1;
                                if (tipPayBox == 1) {
                                    $('.mask .bg').click(function() {
                                        $('.mask').css("display", "none");
                                        $('.tip_pay_box').fadeOut();
                                        tipPayBox = 0;
                                    })
                                }
                                SmallOrderId = data.data.SmallOrderId;
                            } else {
                                $.toptip(data.msg, 'error');
                                return false
                            }
                        },
                        error: function() {
                            $.toptip("加小费接口请求错误", 'error');
                            return false
                        }
                    });
                })
                $('.add_tip_box .cancel').off("click").on("click", function() {
                    $(".mask").css('display', 'none');
                    $('.add_tip_box').css('display', 'none');
                    tipPrice = 0;
                    addPriceScroll.destroy();
                    addPriceScroll = null;
                })
            })
            //小费微信or余额支付
        var wxPay = true;
        var balancePay = false;
        $("#balance_pay").click(function() {
            $('#balance_pay .iconfont').css('display', 'block');
            $('#weixin_pay .iconfont').css('display', 'none');
            wxPay = false;
            balancePay = true;
        })
        $(".weixin_pay").click(function() {
            $('#balance_pay .iconfont').css('display', 'none');
            $('#weixin_pay .iconfont').css('display', 'block');
            wxPay = true;
            balancePay = false;
        })
        var userInfo = localStorage.getItem("userInfo");
        var userInfoObj = JSON.parse(userInfo);
        var UserId = userInfoObj.data.UserId;
        $(".form_submit").click(function() {
                if (balancePay == true && wxPay == false && OrderId != "" && SmallId != "") {
                    //小费余额支付
                    $.ajax({
                        type: "post",
                        url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Update_SmallMoney",
                        data: {
                            "SmallOrderId": SmallOrderId,
                            "UserId": UserId
                        },
                        dataType: "json",
                        beforeSend: function() {
                            //console.log("正在发送支付请求;")
                        },
                        success: function(data) {
                            //console.log(data);
                            if (data.msgcode == "1") {
                                $('.tip_pay_box').css("display", "none");
                                $('.pay_success').css("display", "block");
                                var countDown = parseInt(3);

                                function timeShow() {
                                    countDown--;
                                    $(".count_down").html(countDown);
                                    if (countDown < 1) {
                                        clearInterval(countdown);
                                        $(".count_down").html("倒计时结束！！");
                                        $('.mask').css("display", "none");
                                        $('.pay_success').css("display", "none");
                                        getOrderStatus();
                                    }
                                };
                                countdown = setInterval(timeShow, 1000);
                            } else {
                                $.toptip(data.msg, 'error');
                                return false
                            }
                        },
                        error: function() {
                            $.toptip("余额请求失败", 'error');
                            return false
                        },
                        complete: function() {
                            $('.mask').css("display", "none");
                            $('.tip_pay_box').fadeOut();
                            tipPayBox = 0;
                        }
                    });
                } else if (balancePay == false && wxPay == true && OrderId != "" && SmallId != "") { //微信支付
                    $.ajax({
                        type: 'post',
                        url: currentUrl + '/WxSdjs/PayHandler.ashx',
                        data: {
                            action: 'tip',
                            smallId: SmallId,
                            smallOrderId: 'z' + SmallOrderId,
                            openId: localStorage.getItem("openId")
                        },
                        success: function(response) {
                            var result = JSON.parse(response);
                            if (result.IsOk) {
                                var wxJsApiParam = result.Data;
                                WeixinJSBridge.invoke('getBrandWCPayRequest',
                                    JSON.parse(wxJsApiParam),
                                    function(res) {
                                        if (res.err_msg == "get_brand_wcpay_request:ok") {

                                            $('.mask').css("display", "block");
                                            $('.pay_success').css("display", "block");
                                            isPlaceOrder = true; //限制重复下单；
                                            var countDown = parseInt(3);

                                            function timeShow() {
                                                countDown--;
                                                $(".count_down").html(countDown);
                                                if (countDown < 1) {
                                                    clearInterval(countdown);
                                                    $(".count_down").html("倒计时结束！！");
                                                    $('.mask').css("display", "none");
                                                    getOrderStatus();
                                                }
                                            };
                                            countdown = setInterval(timeShow, 1000);
                                        }
                                        // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                                        //因此微信团队建议，当收到ok返回时，向商户后台询问是否收到交易成功的通知，若收到通知，前端展示交易成功的界面；若此时未收到通知，商户后台主动调用查询订单接口，查询订单的当前状态，并反馈给前端展示相应的界面。
                                    });
                            }

                        },
                        error: function() {
                            $.toptip("微信充值失败", 'error');
                        },
                        complete: function() {
                            $('.mask').css("display", "none");
                            $('.tip_pay_box').fadeOut();
                            tipPayBox = 0;
                        }
                    });
                }
            })
            //获取订单详情(买送取)
        if (sdType === "0" || sdType === "1" || sdType === "2") {
            var orderWait = function ajaxOrderDetail() {
                $.ajax({
                    type: "get",
                    url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_OrderDetail",
                    data: {
                        "OrderId": OrderId
                    },
                    dataType: "json",
                    success: function(data) {
                        // console.log(data);
                        if (data.msgcode == "1") {
                            var orderStatus = data.data.Status;
                            limitTime--;
                            if (limitTime == 0) {
                                clearInterval(t);
                                $.alert("暂未有闪电侠接单");
                            }
                            if (orderStatus == "1") {
                                console.log(orderStatus);
                                $.toptip('闪电侠已接到你的订单', 'success');
                                window.location.href = "../html/order_status.html";
                            }
                        } else {
                            $.toptip(data.msg, 'error');
                            return false
                        }
                    },
                    error: function() {
                        $.toptip("无法获得订单详情", 'error');
                        return false
                    }
                })
            }
        } else if (sdType == "5") {
            var orderWait = function ajaxOrderDetail() {
                $.ajax({
                    type: "get",
                    url: currentUrl + "/interfaceDOC/firstProject/User.ashx?action=Get_DetailBangPai",
                    data: {
                        "OrderId": OrderId
                    },
                    dataType: "json",
                    success: function(data) {
                        if (data.msgcode == "1") {
                            var orderStatus = data.data.Status;
                            limitTime--;
                            if (limitTime == 0) {
                                clearInterval(t);
                                $.alert("暂未有闪电侠接单");
                            }
                            if (orderStatus == "1") {
                                //console.log(orderStatus);
                                $.toptip('闪电侠已接到你的订单', 'success');
                                window.location.href = "../html/order_status.html";
                            }
                        } else {
                            $.toptip(data.msg, 'error');
                            return false
                        }
                    },
                    error: function() {
                        $.toptip("无法获得订单详情", 'error');
                        return false
                    }
                })
            }
        }
        // 定时器跳转
        var t = setInterval(orderWait, 1000);
        var limitTime = 300;
    }) //onload